package com.dc.commons.config.web;

import javax.servlet.FilterRegistration.Dynamic;

import java.util.EnumSet;

import javax.servlet.DispatcherType;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.core.annotation.Order;
import org.springframework.web.WebApplicationInitializer;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

/*******************************************************************************<P>
 * 文件名: DruidWebInitializer.java <br>
 * 版本: 1.0<br>
 * 描述: Druid 监控 配置 可以对请求进行监控 统计<br>
 * 版权所有: <br>
 * <P>
 * 创建者: 王建栋 <br>
 * 创建日期: 2015年12月17日 上午10:01:53<br>
 * 修改者: <br>
 * 修改日期: <br>
 * 修改说明: <br>
 *****************************************************************************
 */
@Order(2)
public class DruidWebInitializer implements WebApplicationInitializer{

	@Override
	public void onStartup(ServletContext servletContext) throws ServletException {
		StatViewServlet servlet=new StatViewServlet();
		ServletRegistration.Dynamic registration = servletContext.addServlet("druid", servlet);
		registration.setLoadOnStartup(1);
		registration.addMapping("/druid/*");
		
		WebStatFilter webStatFilter=new WebStatFilter();
		Dynamic registrationFilter = servletContext.addFilter("druidFilter", webStatFilter);
		
		registrationFilter.addMappingForUrlPatterns(getDispatcherTypes(), false, "/*");
		registrationFilter.setInitParameter("exclusions", "/druid/*,statics/*");


	}
	
	private EnumSet<DispatcherType> getDispatcherTypes() {
		return EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.ASYNC);
	}

}
